import React, { Fragment } from "react";
import { openWebView } from "@tools/native/navigate";
import { Carousel } from "antd-mobile";
import { refresh } from "@component/scroll/index2";
import "./style.scss";

/**
 * @msg: 公共banner组件
 * bannerList:banner列表数组
 * @param {*}
 * @return {*}
 */
/** */
function Banner(props) {
	const handleLink = value => {
		console.log("value", value);
		props.onClick && props.onClick();
		if (value.link) {
			let targetArea = value.targetArea;
			if (typeof value.targetArea === "number") {
				targetArea = value.targetArea + "";
			}
			if (targetArea === "0") {
				//内部
				props.history.push(value.link);
			} else if (targetArea === "1") {
				openWebView({ url: value.link });
			} else if (targetArea == "2") {
				props.history.push({
					pathname: "/iframeAgreements",
					state: {
						src: value.link,
						title: ""
					}
				});
			}
		}
	};
	const { bannerList = [] } = props;
	return (
		<div className="component-banner" {...props}>
			{bannerList.length > 0 && (
				<Fragment>
					{bannerList.length === 1 ? (
						<div className="banner-item ui-flex ui-flex-justify-center">
							<img
								onClick={() => handleLink(bannerList[0])}
								src={bannerList[0].imgUrl}
							/>
						</div>
					) : (
						<Carousel
							className="my-carousel"
							autoplay={true}
							infinite
							dotStyle={{
								width: ".12rem",
								height: ".12rem",
								background: "#fff",
								opacity: 0.5
							}}
							dotActiveStyle={{
								borderRadius: ".12rem",
								width: ".24rem",
								height: ".12rem",
								background: "#fff"
							}}
						>
							{bannerList.map((item, index) => {
								return (
									<div
										className="banner-item ui-flex ui-flex-justify-center"
										key={item.imgUrl}
									>
										<img
											onLoad={refresh}
											src={item.imgUrl}
											onClick={() => handleLink(item)}
										/>
									</div>
								);
							})}
						</Carousel>
					)}
				</Fragment>
			)}
		</div>
	);
}

export default Banner;
